import { _decorator, Asset, Node, Prefab, resources, assetManager, instantiate, Color } from 'cc';
import { UIAlert } from './Alert/UIAlert';
import { UILoading } from './Loading/UILoading';
import { UIManager } from './UIManager';
import { UIBase } from './UIBase';
import { Toast } from './Toast/Toast';


export class CommonUI {
    private static alertPrefab: Prefab = null;
    private static loadingPrefab: Prefab = null;
    private static loadingInstance: Node = null;
    private static initSuccess: boolean = false;

    public static async init(): Promise<boolean> {
        // 初始化UIManager
        const uiManager = UIManager.getInstance();
        if (!await uiManager.init()) {
            return false;
        }

        const bundle = uiManager.getBundle();
        try {
            // 加载Alert预制体
            const [alertPrefab, loadingPrefab] = await Promise.all([
                new Promise<Prefab>((resolve, reject) => {
                    bundle.load('Alert', Prefab, (err, prefab) => {
                        if (err) {
                            console.error(`CommonUI, 加载Alert资源失败:`, err);
                            reject(err);
                        } else {
                            resolve(prefab);
                        }
                    });
                }),
                new Promise<Prefab>((resolve, reject) => {
                    bundle.load('Loading', Prefab, (err, prefab) => {
                        if (err) {
                            console.error(`CommonUI, 加载Loading资源失败:`, err);
                            reject(err);
                        } else {
                            resolve(prefab);
                        }
                    });
                })
            ]);

            this.alertPrefab = alertPrefab;
            this.loadingPrefab = loadingPrefab;
            this.initSuccess = true;
            console.info(`CommonUI, 加载资源成功`);
            return true;
        } catch (error) {
            console.error(`CommonUI, 加载资源失败:`, error);
            return false;
        }
    }

    public static showModal(opt: {
        title?: string,
        content: string,
        showCancel?: boolean,
        cancelText?: string,
        confirmText?: string,
        success?: (result: { confirm: boolean, cancel: boolean }) => void
    }) {
        if (!this.initSuccess) {
            console.warn(`CommonUI, 资源未加载完成, 请稍后再试`);
            return;
        }

        const { title, content, showCancel = true, cancelText = "取消", confirmText = "确定", success } = opt;
        const alert = instantiate(this.alertPrefab);
        UIManager.getInstance().getPopupLayer().addChild(alert);
        let uiAlert = alert.getComponent(UIAlert);

        uiAlert
            .setTitle(title || "提示")
            .setMsg(content)
            .setShowCancel(showCancel)
            .setOkText(confirmText)
            .setCancelText(cancelText);

        if (success) {
            alert.on("ok", () => success({ confirm: true, cancel: false }));
            alert.on("cancel", () => success({ confirm: false, cancel: true }));
        }
    }

    public static showLoading(options: { title: string, mask?: boolean }) {
        if (!this.initSuccess) {
            console.warn(`CommonUI, 资源未加载完成, 请稍后再试`);
            return;
        }

        if (!this.loadingInstance) {
            this.loadingInstance = instantiate(this.loadingPrefab);
            UIManager.getInstance().getLoadingLayer().addChild(this.loadingInstance);
        }
        UILoading.showLoading(options);

    }

    public static hideLoading() {
        if (this.loadingInstance) {
            UILoading.hideLoading();
        }
    }

    
    public static showToast(msg: string, options?: {
        gravity?: 'CENTER' | 'TOP' | 'BOTTOM',
        duration?: number,
        bgColor?: Color
    }) {
        if (!this.initSuccess) {
            // Guyu.Log.warn(`CommomUI, 资源未加载完成, 请稍后再试`);
            return;
        }
      Toast.show("已经检查了Toast.ts");
    }

}